import 'package:flutter/material.dart';
import 'package:flutter_bluebell/screens/chat_page.dart';


import '../screens/auth/login_screen.dart';
import '../screens/auth/signup_screen.dart';
import '../screens/home/home_screen.dart';
import '../screens/my/collection_list_page.dart';
import '../screens/my/history_list_page.dart';
import '../screens/my/my_page.dart';
import '../screens/my/user_posts_page.dart';
import '../screens/post_detail_screen.dart';
import '../screens/publish_post_screen.dart';

class AppRoutes {
  static const String loginScreen = '/login';
  static const String signupScreen = '/signup';
  static const String mainScreen = '/'; // 首页，包含Tabs
  static const String postDetailScreen = '/postDetail';
  static const String communityDetailScreen = '/communityDetail';
  static const String publishPostScreen = '/publishPost';
  static const String myPage = '/myPage'; 
  static const String chatPage = '/chatPage'; 
  static const String userPosts = '/userPosts'; 
  // 添加的页面路由
  static const String collectedPosts = '/collectedPosts'; // 我的收藏页面
  static const String history = '/history'; // 浏览历史页面
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case userPosts:
        return MaterialPageRoute(builder: (_) => const UserPostsScreen());
      case chatPage:
        return MaterialPageRoute(builder: (_) => const ChatPage());
      case loginScreen:
        return MaterialPageRoute(builder: (_) => const LoginScreen());
      case signupScreen:
        return MaterialPageRoute(builder: (_) => const SignupScreen());
      case mainScreen:
        return MaterialPageRoute(builder: (_) => const MainTabScreen()); // 主Tab页面
      case postDetailScreen:
        if (settings.arguments is String) { // 假设帖子详情页接收post_id
          final postId = settings.arguments as String;
          return MaterialPageRoute(
            builder: (_) => PostDetailScreen(postId: postId),
          );
        }
        return _errorRoute('无效的帖子ID参数');
      case collectedPosts: // 新增收藏页面路由
        return MaterialPageRoute(builder: (_) => const CollectionListPage());
      case history: // 新增历史页面路由
        return MaterialPageRoute(builder: (_) => const HistoryListScreen());
      case publishPostScreen:
        return MaterialPageRoute(builder: (_) => const PublishPostScreen());

      default:
        return _errorRoute('未定义路由: ${settings.name}');
    }
  }

  static MaterialPageRoute _errorRoute(String message) {
    return MaterialPageRoute(
      builder: (_) => Scaffold(
        appBar: AppBar(title: const Text('错误')),
        body: Center(child: Text(message)),
      ),
    );
  }
}



// MainTabScreen: 包含首页和我的页面
class MainTabScreen extends StatefulWidget {
  const MainTabScreen({super.key});

  @override
  State<MainTabScreen> createState() => _MainTabScreenState();
}

class _MainTabScreenState extends State<MainTabScreen> {
  int _selectedIndex = 0;

  static final List<Widget> _widgetOptions = <Widget>[
    const HomeScreen(), // 首页
    const MyPage(),     // 我的页面
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _widgetOptions.elementAt(_selectedIndex),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.blue,
        onTap: _onItemTapped,
      ),
    );
  }
}